/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainPlanDayCount;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.vo.MaintainPlanDayCountVO;
import org.springblade.modules.admin.mapper.MaintainPlanDayCountMapper;
import org.springblade.modules.admin.service.IMaintainPlanDayCountService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 任务每天 服务实现类
 *
 * @author ysq
 * @since 2024-09-02
 */
@AllArgsConstructor
@Service
public class MaintainPlanDayCountServiceImpl extends BaseServiceImpl<MaintainPlanDayCountMapper, MaintainPlanDayCount> implements IMaintainPlanDayCountService {

	private final IMaintainPlanContentService planContentService;

	@Override
	public IPage<MaintainPlanDayCountVO> selectMaintainPlanDayCountPage(IPage<MaintainPlanDayCountVO> page, MaintainPlanDayCountVO maintainPlanDayCount) {
		return page.setRecords(baseMapper.selectMaintainPlanDayCountPage(page, maintainPlanDayCount));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateWxRescuePlanDayCount() {

		List<MaintainPlanDayCount> dayCountList = baseMapper.selectWxRescuePlanDayCount();
		if(Func.isNotEmpty(dayCountList)){
			dayCountList.forEach(e -> e.setRecordDate(DateUtil.offsetDay(DateUtil.date(),-1)));
			List<List<MaintainPlanDayCount>> resultList = ListUtil.partition(dayCountList,20);
			resultList.forEach(super::saveBatch);
		}
	}

}
